package com.shark.uh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.shark.uh.model.vo.UserVO;
import com.shark.uh.model.dto.user.UserBindDTO;
import com.shark.uh.model.dto.user.UserLoginDTO;
import com.shark.uh.model.dto.user.UserCreateDTO;
import com.shark.uh.model.dto.user.UserUpdateDTO;
import com.shark.uh.common.ApiResponse;
import com.shark.uh.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


@Log4j2
@RestController
@Validated
public class UserController {


    @Resource
    private UserService userService;



    /**
     * 退出登录
     *
     * @return ApiResponse
     */
    @GetMapping("api/user/logout")
    @SaCheckLogin
    public ApiResponse<Object> logout() {
        StpUtil.logout();
        return ApiResponse.success();
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("api/user/get-info")
    @SaCheckLogin
    public ApiResponse<UserVO> getUserInfo() {
        UserVO info = userService.getUserInfo();
        return ApiResponse.success(info);
    }


    /**
     * 更新用户信息，username、phone、email除外。
     *
     * @param request 更新请求
     * @return ApiResponse
     */
    @PostMapping("api/user/update")
    @SaCheckLogin
    public ApiResponse<Object> update(@RequestBody UserUpdateDTO request) {
        userService.updateUser(request);
        return ApiResponse.success();
    }




    /**
     * 绑定联系方式
     *
     * @param request 请求体
     * @return ApiResponse
     */
    @PostMapping("api/user/bind")
    @SaCheckLogin
    public ApiResponse<Object> bind(@RequestBody @Valid UserBindDTO request) {
        userService.bind(request);
        return ApiResponse.success();
    }

    /**
     * 解绑联系方式
     *
     * @param request 请求
     * @return ApiResponse
     */
    @PostMapping("api/user/un-bind")
    @SaCheckLogin
    public ApiResponse<Object> unbind(@RequestBody @Valid UserBindDTO request) {
        userService.unbind(request);
        return ApiResponse.success();
    }


}
